import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/common_models/clothing_model.dart';

/// 删除 商品 item
class DelClothingItemWidget extends StatelessWidget {
  final ClothingModel model;

  final Function onDelete;

  const DelClothingItemWidget({Key key, @required this.model, this.onDelete}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ClothingItemWidget(
      model: this.model,
      suffix: Stack(
        children: [
          Align(
            alignment: Alignment.centerRight,
            child: BaseCloseButton(onPress: this.onDelete),
          )
        ],
      ),
    );
  }
}

/// 选择状态 商品 item
class CheckboxClothingItemWidget extends StatefulWidget {
  final ClothingModel model;

  final bool isSelected;

  final Function(bool) onChange;

  const CheckboxClothingItemWidget({Key key, this.model, this.isSelected = false, this.onChange}) : super(key: key);

  @override
  _CheckboxClothingItemWidgetState createState() => _CheckboxClothingItemWidgetState();
}

class _CheckboxClothingItemWidgetState extends State<CheckboxClothingItemWidget> with AutomaticKeepAliveClientMixin {
  bool _selected;

  @override
  void initState() {
    _selected = widget.isSelected;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant CheckboxClothingItemWidget oldWidget) {
    if (_selected != widget.isSelected) {
      _selected = widget.isSelected;
    }
    if (oldWidget.isSelected != widget.isSelected) {
      _selected = widget.isSelected;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseCardComponent(
      onTap: () {
        _selected = !_selected;
        widget.onChange(_selected);
        setState(() {});
      },
      child: ClothingItemWidget(
        model: widget.model,
        suffix: BaseCustomCheckboxButton.tile(checked: _selected, disable: true),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

/// 通用 商品 item
class ClothingItemWidget extends StatelessWidget {
  final ClothingModel model;

  final Widget suffix;

  ClothingItemWidget({Key key, this.suffix, @required this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Row(
        children: [
          BaseImageComponent(imageUrl: this.model?.clothingImg, width: 72, height: 72, circular: 4, hero: this.hashCode.toString()),
          SizedBox(width: 12),
          Expanded(child: _buildContent(context)),
          this.suffix ?? SizedBox(),
        ],
      ),
    );
  }

  /// 详情
  Widget _buildContent(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(this.model.styleName ?? '', style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 14)),
        SizedBox(height: 6),
        BaseComponentUtils.buildTextSpanWidget([this.model.styleId, this.model.colorName, this.model.sizeName]),
        this.model.priceTag != null ? Container(margin: EdgeInsets.only(top: 6), child: Text('吊牌价：￥ ${this.model.priceTag.toStringAsFixed(2)}', style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 12))) : SizedBox(),
        this.model.remark != null ? Container(margin: EdgeInsets.only(top: 6), child: Text(this.model.remark)) : SizedBox(),
      ],
    );
  }
}
